package com.itheima.controller;


import com.itheima.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class AjaxController {


    @RequestMapping("/ajaxController")
    public String ajaxController(@RequestBody String message) {
        System.out.println("ajax request is running..." + message);
        return "page.jsp";
    }


    @RequestMapping("/ajaxPojoToController")
    public String ajaxPojoToController(@RequestBody User user) {
        System.out.println("pojo Controller :" + user);
        return "page.jsp";
    }


    @RequestMapping("/ajaxListToController")
    public String ajaxListToController(@RequestBody List<User> userList){
        System.out.println("controller list :"+userList);
        return "page.jsp";
    }


    @RequestMapping("/ajaxReturnString")
    //@ResponseBody
    public @ResponseBody  String ajaxReturnString(){
        System.out.println("controller returning string...");
        return "page.jsp";
    }

    @RequestMapping("/ajaxReturnJson")
    @ResponseBody
    public User ajaxReturnJson(){
        System.out.println("controller return json pojo...");
        User  user = new User();
        user.setName("jock");
        user.setAge(18);
        return user;
    }


    @RequestMapping("/ajaxReturnJsonList")
    @ResponseBody
    public List ajaxReturnJsonList(){
        System.out.println("controller return json list...");
        User user1 = new User();
        user1.setName("Tom");
        user1.setAge(18);

        User user2 = new User();
        user2.setName("Jerry");
        user2.setAge(18);

        ArrayList al  = new ArrayList();
        al.add(user1);
        al.add(user2);

        return al;

    }


    @RequestMapping("/cross")
    @ResponseBody
    @CrossOrigin
    public User cross(HttpServletRequest request){
        System.out.println("controller cross..."+request.getRequestURL());
        User user = new User();
        user.setName("jock");
        user.setAge(18);
        return user;
    }

}
